package com.changgou.service.system.controller;


import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.service.system.service.AdminService;
import com.changgou.service.system.util.JwtUtil;
import com.changgou.system.pojo.Admin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Resource
    private AdminService adminService;


    //登录

    @PostMapping("/login")
    public Result<?> login(@RequestBody Admin admin) {
        boolean login = adminService.login(admin);
        if (login) {  //如果验证成功
            Map<String, String> info = new HashMap<>();
            info.put("username", admin.getLoginName());
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), admin.getLoginName(), null);
            info.put("token", token);
            return new Result<Map<String, String>>(true, StatusCode.OK, "登录成功", info);
        } else {
            return new Result<Void>(false, StatusCode.LOGINERROR, "用户名或密码错误");
        }
    }
}
